#include<cstdlib>
#include<iostream>
#include<cmath>
#include<complex>

#include<formic/fqmc/fqmc.h>
//#include<formic/fqmc/input.h>
//#include<formic/all_data.h>
//#include<formic/command/command.h>
//#include<formic/output/output.h>
//#include<formic/random/random.h>
//#include<formic/numeric/numeric.h>
//#include<formic/timing/timing.h>
//#include<formic/mpi/interface.h>
#include<formic/test_driver/utilities.h>
#include<formic/exception.h>

int main(int argc, char ** argv) {

  const int retval = formic::fqmc::main(argc, argv);
  if (retval != EXIT_SUCCESS)
    return retval;

  const double rhf_energy = -74.963334808181;

  // check psi3 hartree fock energy
  {
    std::ifstream ifs("output.dat");
    std::string line;
    formic::test::get_line_containing_str(ifs, line, "* SCF total energy   =");
    ifs.unget();
    while (ifs && ifs.peek() != '=')
      ifs.unget();
    ifs.get();
    double val = 0.0;
    ifs >> val;
    if ( std::abs(rhf_energy - val) > 1.0e-8 )
      throw formic::Exception("psi3 rhf energy is incorrect");
    ifs.close();
  }

  // check formic hartree fock energy
  {
    std::ifstream ifs("output.txt");
    std::string line;
    formic::test::get_line_containing_str(ifs, line, "Hartree Fock energy =");
    ifs.unget();
    while (ifs && ifs.peek() != '=')
      ifs.unget();
    ifs.get();
    double val = 0.0;
    ifs >> val;
    if ( std::abs(rhf_energy - val) > 1.0e-8 )
      throw formic::Exception("formic rhf energy is incorrect");
    ifs.close();
  }

  // check exact sampling monte carlo hartree fock energy
  {
    std::ifstream ifs("output.txt");
    std::string line;
    formic::test::get_line_containing_str(ifs, line, "mean =");
    ifs.unget();
    while (ifs && ifs.peek() != '=')
      ifs.unget();
    ifs.get();
    double val = 0.0;
    ifs >> val;
    if ( std::abs(rhf_energy - val) > 1.0e-8 )
      throw formic::Exception("formic exact sampling monte carlo rhf energy is incorrect");
    ifs.close();
  }

//  // check monte carlo energy of optimized agp
//  {
//    std::ifstream ifs("output.txt");
//    std::string line;
//    formic::test::get_line_containing_str(ifs, line, "mean =");
//    formic::test::get_line_containing_str(ifs, line, "mean =");
//    ifs.unget();
//    while (ifs && ifs.peek() != '=')
//      ifs.unget();
//    ifs.get();
//    double val = 0.0;
//    ifs >> val;
//    if ( std::abs(-74.980456253580 - val) > 1.0e-8 )
//      throw formic::Exception("formic monte carlo energy of optimized agp is incorrect");
//    ifs.close();
//  }
//
//  // check exact sampling RHF energy
//  {
//    std::ifstream ifs("output.txt");
//    std::string line;
//    formic::test::get_line_containing_str(ifs, line, "mean =");
//    formic::test::get_line_containing_str(ifs, line, "mean =");
//    formic::test::get_line_containing_str(ifs, line, "mean =");
//    ifs.unget();
//    while (ifs && ifs.peek() != '=')
//      ifs.unget();
//    ifs.get();
//    double val = 0.0;
//    ifs >> val;
//    if ( std::abs(rhf_energy - val) > 1.0e-8 )
//      throw formic::Exception("exact sampling RHF energy incorrect");
//    ifs.close();
//  }

  return EXIT_SUCCESS;

}
